package mathgame.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;


public class ExamplePanel extends GamePanel {
    private JButton jBackBtn, jEquationsBtn, jAdditionBtn, jDivisionBtn, jMultiplicationBtn,
            jSubtractionBtn, jPercentagesBtn, jNextBtn, jPreviousBtn;
    private JLabel jInfoLbl, jImageLbl;
    private JPanel chapterButtonPanel;
    private ArrayList<ImageIcon> pageList;
    private int pageNumber;

    public ExamplePanel(){
        pageList = new ArrayList<ImageIcon>();
        readInPages();
        this.setLayout(null);
        chapterButtonPanel = new JPanel();
        chapterButtonPanel.setLayout(null);
        icon = createImageIcon("../source/StartPanel2.png", "background");
        jBackBtn = new JButton();
        jBackBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jBackBtn.setText("BACK");
        jEquationsBtn = new JButton();
        jEquationsBtn.setText("Equations");
        jEquationsBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jEquationsBtn.setForeground(new Color(99,184,255));
        jAdditionBtn = new JButton();
        jAdditionBtn.setText("Addition");
        jAdditionBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jAdditionBtn.setForeground(new Color(99,184,255));
        jDivisionBtn = new JButton();
        jDivisionBtn.setText("Division");
        jDivisionBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jDivisionBtn.setForeground(new Color(99,184,255));
        jMultiplicationBtn = new JButton();
        jMultiplicationBtn.setText("Multiplication");
        jMultiplicationBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jMultiplicationBtn.setForeground(new Color(99,184,255));
        jSubtractionBtn = new JButton();
        jSubtractionBtn.setText("Subtraction");
        jSubtractionBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jSubtractionBtn.setForeground(new Color(99,184,255));
        jPercentagesBtn = new JButton();
        jPercentagesBtn.setText("Percentage");
        jPercentagesBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jPercentagesBtn.setForeground(new Color(99,184,255));
        jInfoLbl = new JLabel();
        jInfoLbl.setText("<html>Here you can get some help...<br>choose chapter below!</html>");
        jInfoLbl.setFont(new Font("monospaced", Font.BOLD, 25));
        jNextBtn = new JButton();
        jNextBtn.setText("Next");
        jNextBtn.setFont(new Font("monospaced", Font.BOLD, 15));
        jPreviousBtn = new JButton();
        jPreviousBtn.setText("Previous");
        jPreviousBtn.setFont(new Font("monospaced", Font.BOLD, 15));
        jImageLbl = new JLabel();

        jBackBtn.addActionListener(actionListener);
        jAdditionBtn.addActionListener(actionListener);
        jSubtractionBtn.addActionListener(actionListener);
        jEquationsBtn.addActionListener(actionListener);
        jMultiplicationBtn.addActionListener(actionListener);
        jDivisionBtn.addActionListener(actionListener);
        jPercentagesBtn.addActionListener(actionListener);
        jPreviousBtn.addActionListener(actionListener);
        jNextBtn.addActionListener(actionListener);


        chapterButtonPanel.add(jAdditionBtn);
        chapterButtonPanel.add(jDivisionBtn);
        chapterButtonPanel.add(jEquationsBtn);
        chapterButtonPanel.add(jMultiplicationBtn);
        chapterButtonPanel.add(jPercentagesBtn);
        chapterButtonPanel.add(jSubtractionBtn);
        
        this.add(chapterButtonPanel);
        this.add(jExitBtn);
        this.add(jHelpBtn);
        this.add(jBackBtn);
        this.add(jInfoLbl);
        this.add(jPreviousBtn);
        this.add(jNextBtn);
        this.add(jImageLbl);

        Insets insets = this.getInsets();
        Insets insets2 = chapterButtonPanel.getInsets();
        int WIDTH_SMALL = 150;
        int WIDTH_LARGE = 200;
        int HEIGHT_SMALL = 40;
        int HEIGHT_LARGE = 70;

        jBackBtn.setBounds(440 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        jExitBtn.setBounds(800 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        jHelpBtn.setBounds(620 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        chapterButtonPanel.setBounds(280 + insets.left, 20 + insets.top, 680, 500);
        jAdditionBtn.setBounds(insets2.left, 280 + insets2.top, WIDTH_LARGE, HEIGHT_LARGE);
        jDivisionBtn.setBounds(insets2.left, 380 + insets2.top, WIDTH_LARGE, HEIGHT_LARGE);
        jEquationsBtn.setBounds(240 + insets2.left, 380 + insets2.top, WIDTH_LARGE, HEIGHT_LARGE);
        jMultiplicationBtn.setBounds(240 + insets2.left, 280 + insets2.top, WIDTH_LARGE, HEIGHT_LARGE);
        jPercentagesBtn.setBounds(480 + insets2.left, 280 + insets2.top, WIDTH_LARGE, HEIGHT_LARGE);
        jSubtractionBtn.setBounds(480 + insets2.left, 380 + insets2.top, WIDTH_LARGE, HEIGHT_LARGE);
        jInfoLbl.setBounds(480 + insets.left, 50 + insets.top, 520, HEIGHT_LARGE);
        jPreviousBtn.setBounds(600 + insets.left, 480 + insets.top, 100, 30);
        jNextBtn.setBounds(840 + insets.left, 480 + insets.top, 100, 30);
        jImageLbl.setBounds(550 + insets.left, 10 + insets.top, 420, 500);
        jImageLbl.setVisible(false);
        jPreviousBtn.setVisible(false);
        jNextBtn.setVisible(false);


        chapterButtonPanel.setOpaque(false);
        this.setOpaque(false);
    }

    protected void paintComponent(Graphics g){
        g.drawImage(icon.getImage(), 0, 0, null);
        super.paintComponent(g);
    }

    ActionListener actionListener = new ActionListener(){

        public void actionPerformed(ActionEvent e){

            if(e.getSource() == jBackBtn){
                controller.changeCard(1);
                chapterButtonPanel.setVisible(true);
                jImageLbl.setVisible(false);
                jPreviousBtn.setVisible(false);
                jNextBtn.setVisible(false);
                jInfoLbl.setVisible(true);
            }
            else if(e.getSource()== jSubtractionBtn){
                setChapter("subtraction");
            }
            else if(e.getSource()== jAdditionBtn){
                setChapter("addition");
            }
            else if(e.getSource()== jMultiplicationBtn){
                setChapter("multiplication");
            }
            else if(e.getSource()== jDivisionBtn){
                setChapter("division");
            }
            else if(e.getSource()== jEquationsBtn){
                setChapter("equations");
            }
            else if(e.getSource()== jPercentagesBtn){
                setChapter("percentage");
            }
            else if(e.getSource()== jPreviousBtn){
            	
            	if (pageNumber > 0)

                    pageNumber--;
                    changePage();

            }
            else if(e.getSource()== jNextBtn){
                if(pageNumber<pageList.size()-1){
                    pageNumber++;
                    changePage();
                }
            }
        }
    };

    public void changePage(){
        jImageLbl.setIcon(pageList.get(pageNumber));
    }

    public void readInPages(){
        ImageIcon equations_1 = createImageIcon("../source/sida1.png", "sida1");
        ImageIcon equations_2 = createImageIcon("../source/sida2.png", "sida2");
        ImageIcon equations_3 = createImageIcon("../source/sida3.png", "sida3");
        ImageIcon equations_4 = createImageIcon("../source/sida4.png", "sida4");
        ImageIcon equations_5 = createImageIcon("../source/sida5.png", "sida5");
        ImageIcon addition_1 = createImageIcon("../source/addition1.png", "addition1");
        ImageIcon subtract_1 = createImageIcon("../source/subtract1.png", "subtract1");
        ImageIcon division_1 = createImageIcon("../source/division1.png", "division1");
        ImageIcon multiplication_1 = createImageIcon("../source/multiplication1.png", "multiplication1");
        ImageIcon percent_1 = createImageIcon("../source/percent1.png", "percent1");
        ImageIcon percent_2 = createImageIcon("../source/percent2.png", "percent2");
        ImageIcon percent_3 = createImageIcon("../source/percent3.png", "percent3");
        ImageIcon percent_4 = createImageIcon("../source/percent4.png", "percent4");
        ImageIcon percent_5 = createImageIcon("../source/percent5.png", "percent5");
        
        pageList.add(equations_1);
        pageList.add(equations_2);
        pageList.add(equations_3);
        pageList.add(equations_4);
        pageList.add(equations_5);
        pageList.add(addition_1);
        pageList.add(subtract_1);
        pageList.add(division_1);
        pageList.add(multiplication_1);
        pageList.add(percent_1);
        pageList.add(percent_2);
        pageList.add(percent_3);
        pageList.add(percent_4);
        pageList.add(percent_5);
        
        
    }

    public void setChapter(String chapter){
        chapterButtonPanel.setVisible(false);
    	jPreviousBtn.setVisible(true);
        jNextBtn.setVisible(true);
        jImageLbl.setVisible(true);
        jInfoLbl.setVisible(false);

        if(chapter.equals("equations")){
 
        	pageNumber = 0;
        }
        else if(chapter.equals("addition")){
            pageNumber = 5;
        }
        else if(chapter.equals("subtraction")){
            pageNumber = 6;
        }
        else if(chapter.equals("multiplication")){
            pageNumber = 8;
        }
        else if(chapter.equals("division")){
            pageNumber = 7;
        }
        else if(chapter.equals("percentage")){

        	pageNumber = 9;
            
        }
        jImageLbl.setIcon(pageList.get(pageNumber));
    }
}
